package com.hzgj.bcl.soa.util;

public class Constants {

    public static final int DEFAULT_PAYLOAD = 8 * 1024 * 1024;                      // 8M

    public static final boolean DEFAULT_CLUSTER_AVAILABLE_CHECK = true;
    public static final boolean DEFAULT_REGISTRY = true;

    public static final String MESSAGE_HEADER_TARGET_SERVICE = "message.header.target.service";
    public static final String MESSAGE_SID = "message.sid";
    public static final String MESSAGE_TYPE = "message.type";
    public static final String MESSAGE_ADDRESS_SRC = "message.adddress.src";

    public static final String MESSAGE_HEADER_ASYN = "message.header.asyn";
    public static final String MESSAGE_HEADER_METHOD = "message.header.method";
    public static final String MESSAGE_HEADER_LBRULE = "message.header.lbRule";
    public static final String MESSAGE_HEADER_BEGIN = "message.header.begin";
    public static final String MESSAGE_HEADER_REQUESTTIMEOUTINMILLS = "message.header.requestTimeoutInMills";
    public static final String MESSAGE_HEADER_PROTOCOL = "message.header.protocol";
    public static final String MESSAGE_HEADER_CLUSTER = "message.header.cluster";
    public static final String MESSAGE_HEADER_RETRYPOLICY = "message.header.retryPolicy";

    public static final String MESSAGE_HEADER_SERIALIZATION_NAME = "message.header.serialization";
    public static final String MESSAGE_HEADER_SERIALIZATION_ID = "message.header.serialization.id";
    public static final String MESSAGE_HEADER_EVENT = "message.header.event";
    public static final String MESSAGE_HEADER_SERIALIZATION_BYPASS = "message.header.serialization.bypass";
    public static final String MESSAGE_HEADER_RETRY_STATE = "message.header.retry";
    public static final String MESSAGE_HEADER_ATTACHMENT = "message.header.attachment";

    public static final String MESSAGE_HEADER_REMOTE_HOST = "message.header.remote.host";
    public static final String MESSAGE_HEADER_REMOTE_PORT = "message.header.remote.port";
    public static final String MESSAGE_HEADER_SERVER_HOST = "message.header.server.host";
    public static final String MESSAGE_HEADER_SERVER_PORT = "message.header.server.port";
    public static final String MESSAGE_HEADER_METHOD_PARAMETERTYPES = "message.header.method.argsTypes";
    public static final String MESSAGE_HEADER_METHOD_ARGS = "message.header.method.args";
    public static final String MESSAGE_HEADER_SERVICE_VERSION = "message.header.service.version";
    public static final String MESSAGE_HEADER_SERVICE_GROUP = "message.header.service.group";

    public static final String DEFAULT_PROTOCOL = "default";

    public static final String OBJECTNAME_CHANNELHANDLER = "ChannelHandler";
    public static final String OBJECTNAME_INTERCEPTOR = "Interceptor";
    public static final String OBJECTNAME_CICADA = "Cicada";
    public static final String OBJECTNAME_STATS = "Stats";
    public static final String OBJECTNAME_RPC = "rpc";
    public static final String OBJECTNAME_PREFIX = "com.hzgj.soa:type=";

    public static final String PREFIX = "com.hzgj.bcl.soa";


    public static final int BYTE_SIZE = Byte.SIZE / 8; // 1
    public static final int SHORT_SIZE = Short.SIZE / 8; // 2
    public static final int INT_SIZE = Integer.SIZE / 8; // 4
    public static final int LONG_SIZE = Long.SIZE / 8; // 8
    public static final int DOUBLE_SIZE = Double.SIZE / 8; // 8;
    public static final int FLOAT_SIZE = Float.SIZE / 8; // 4;

    public static final String IPv4 = "java.net.preferIPv4Stack";
    public static final String IPv6 = "java.net.preferIPv6Addresses";

    public static final long THREAD_SHUTDOWN_WAIT_TIME = 300;

    public static final String ETCD_REGISTRY = "etcd";

    public static final String ZK_REGISTRY = "zookeeper";


    public static final int DEFAULT_CONNECTTIMEOUT = 2000;
    public static final int DEFAULT_REQUESTTIMEOUT = 3000;
    public static final int DEFAULT_MAXCONNECTIONS = 200;
    public static final String DEFAULT_LOADBALANCE = "roundRobin";
    public static final int DEFAULT_RETRIES = -1;
    public static final int DEFAULT_MSBEFORERETRY = 20;
    public static final String DEFAULT_CLUSTER_FAILOVER = "failover";
    public static final String CLUSTER_FAILFAST = "failfast";
    public static final String CLUSTER_FAILSAFE = "failsafe";

    public static final String NOTIFY_CLIENT_PULL_MODE = "pull";
    public static final String NOTIFY_CLIENT_PUSH_MODE = "push";

    public static boolean getPropertyAsBoolean(String property, boolean defaultValue) {
        boolean result = defaultValue;
        try {
            String tmp = System.getProperty(property);
            if (tmp != null) {
                result = Boolean.parseBoolean(tmp);
            }
        } catch (Throwable t) {
        }
        return result;
    }

    public static long getPropertyAsLong(String property, long defaultValue) {
        long result = defaultValue;
        try {
            String tmp = System.getProperty(property);
            if (tmp != null) {
                result = Long.parseLong(tmp);
            }
        } catch (Throwable t) {
        }
        return result;
    }

    public static int getPropertyAsInteger(String property, int defaultValue) {
        int result = defaultValue;
        try {
            String tmp = System.getProperty(property);
            if (tmp != null) {
                result = Integer.parseInt(tmp);
            }
        } catch (Throwable t) {
        }
        return result;
    }

    public static String getConnectionHandlerObjectName(int port) {
        return OBJECTNAME_PREFIX + OBJECTNAME_CHANNELHANDLER + ",name = ConnectionHandler_" + port;
    }

    public static String getRpcServerObjectName(String port) {
        return OBJECTNAME_PREFIX + OBJECTNAME_RPC + ",name = RpcServer,port = " + port;
    }

    public static String getCicadaRouterObjectName() {
        return OBJECTNAME_PREFIX + OBJECTNAME_CICADA + ",name = CicadaRouter";
    }

    public static String getRegistryServiceObjectName() {
        return OBJECTNAME_PREFIX + "registry,name=RegistryService";
    }

}
